﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_ReaderReturnBook : System.Web.UI.Page
{
    public string readerID;
    public string bookID;
    public string tips;
    public string bookname, readername;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void sureBorrowAgain_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in mybookdata.Rows)
        {
            Control ctrl = row.FindControl("CheckBox");

            if ((ctrl as CheckBox).Checked)
            {
                

            }

        }



    }
    protected void Cancel_Click(object sender, EventArgs e)
    {

    }
    protected void chaxun_Click(object sender, EventArgs e)
    {
        bookID = TBbookid.Text;
        readerID = TextBox1.Text;

        SqlDataSource1.SelectCommand = "select DDNUM as 流水号, book_info.bookid as 图书编号,bookname as 图书名称,writer as 作者 , Price as 价格, Reader_info.ReaderID as 读者编号,ReaderName as 读者姓名 ,LendBookDate as 借出时间,ShouldReturnDate as 应还时间,datediff(day,LendBookDate,getdate()) as 借阅时长,datediff(day,LendBookDate,getdate())*0.1 as 租金,CASE  WHEN datediff(day,ShouldReturnDate,getdate())>0 THEN datediff(day,ShouldReturnDate,getdate()) ELSE 0 END as 超期天数,Amerce as 超期罚款  from Book_info,Publish_company ,Reader_info,Borrow_books_manage where Publish_company.PublishCompanyID=Book_info.PublishCompanyID  and Borrow_books_manage.BookID='"+bookID+"' and ReturnDate is null and Reader_info.ReaderID=Borrow_books_manage.ReaderID and Book_info.BookID=Borrow_books_manage.BookID";
       SqlDataSource1.SelectParameters["name"].DefaultValue = bookID;
      SQL mysql=new SQL();
      bookname = mysql.EXQSQL("select bookname from book_info where bookid='" + bookID + "'");
      readername = mysql.EXQSQL("select readerid from Borrow_books_manage where bookid='" + bookID + "' and returndate is null");



    }
    protected void sureBorrow_Click(object sender, EventArgs e)
    {


        foreach (GridViewRow row in mybookdata.Rows)
        {
            Control ctrl = row.FindControl("CheckBox");

            if ((ctrl as CheckBox).Checked)
            {
                SQL sql = new SQL();
                bookID = TBbookid.Text;
                readerID = TextBox1.Text;
                /*查询订单号码*/
                string ddm = sql.EXQSQL("select DDNUM from Book_info,Publish_company ,Reader_info,Borrow_books_manage where Publish_company.PublishCompanyID=Book_info.PublishCompanyID  and Borrow_books_manage.BookID='" + bookID + "' and ReturnDate is null and Reader_info.ReaderID=Borrow_books_manage.ReaderID and Book_info.BookID=Borrow_books_manage.BookID");
                /*更新归还日期*/
                sql.EXQSQL("update Borrow_books_manage set ReturnDate='" + DateTime.Now.ToString() + "' where DDNUM='"+ddm+"'");
                /*更新图书状态和图书数量*/
                int booknum = Convert.ToInt32(sql.getDataFormTable("CURRENTNUM", "book_info", "bookID", bookID, "", "", "", "", 1));
                sql.EXQSQL("update Book_info set CurrentNum=" + (booknum + 1) + ",statusid=1 where bookid=" + bookID + "");
                /*更新读者的账户余额*/
                //首先取得读者ID
                readerID = sql.EXQSQL("select readerid from Borrow_books_manage where DDNUM ='" + ddm + "'");
                //获取读者目前的账户余额
                double yuenow = Convert.ToDouble(sql.EXQSQL("select Account from Reader_info where ReaderID='" + readerID + "'"));
                //获取还书日期和借阅日期的时间差：
                int shijiancha = Convert.ToInt32(sql.EXQSQL("select datediff(d,LendBookDate,'" + DateTime.Now.ToString() + "') from  Borrow_books_manage where   DDNUM='" + ddm + "'"));
                //获取系统设置的每天借阅的收费：
                double meirishoufei = Convert.ToDouble(sql.EXQSQL("select daymoney from System_state"));
                //计算读者在借阅过程中一共产生了多少费用,每日收费*时间差：
                double total = meirishoufei * shijiancha; 
                //在读者账户余额中扣除这个钱,与更新罚款公用一个sql函数
                sql.updateAmerce("update Reader_info set Account='" + (yuenow - total) + "' where readerid='" + readerID + "'");
                //提示信息
                tips = "<div class=\"alert alert-success\"><strong>归还成功!</strong> 读者  " + readerID + "   成功归还图书  《" + bookname + " 》。扣除金额："+total+",账户余额为："+(yuenow-total)+"</div>";
            }
        }

        Response.Write("<script language=javascript>alert('归还成功');window.location = 'ReaderReturnBook.aspx';</script>");
    }

    protected void sureAme_Click(object sender, EventArgs e)
    {

    }
}